home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / devices / modm0dev.zoo / syslog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-09  |  7.8 KB  |  346 lines

  1. /*
  2.  * Copyright (c) 1983, 1988 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. #if defined(LIBC_SCCS) && !defined(lint)
  19. static char sccsid[] = "@(#)syslog.c    5.16 (Berkeley) 6/27/88";
  20. #endif /* LIBC_SCCS and not lint */
  21.  
  22. /*
  23.  * SYSLOG -- print message on log file
  24.  *
  25.  * This routine looks a lot like printf, except that it
  26.  * outputs to the log file instead of the standard output.
  27.  * Also:
  28.  *    adds a timestamp,
  29.  *    prints the module name in front of the message,
  30.  *    has some other formatting types (or will sometime),
  31.  *    adds a newline on the end of the message.
  32.  *
  33.  * The output of this routine is intended to be read by /etc/syslogd.
  34.  *
  35.  * Author: Eric Allman
  36.  * Modified to use UNIX domain IPC by Ralph Campbell
  37.  * Modified to use fifo's when running under MiNT on the Atari ST/TT/Falcon
  38.  *    by Stephen Usher
  39.  * Modified to be more ANSI-conformant by Thierry Bousch
  40.  */
  41.  
  42. #include <sys/types.h>
  43. #ifndef atarist
  44. #include <sys/socket.h>
  45. #ifdef SYSLOG_INET
  46. #  include <netinet/in.h>
  47. #else
  48. #  ifdef SYSLOG_UNIXAF
  49. #    include <sys/un.h>
  50. #  else
  51. #    error "Must defined one of SYSLOG_INET or SYSLOG_UNIXAF"
  52. #  endif
  53. #endif
  54. #else
  55. #include <mintbind.h>
  56. #endif
  57.  
  58. #include <sys/file.h>
  59. #include <sys/signal.h>
  60. #include <syslog.h>
  61. #ifndef atarist
  62. #include <netdb.h>
  63. #endif
  64. #include <strings.h>
  65. #ifdef SYSV
  66. #include <fcntl.h>
  67. #endif
  68.  
  69. #ifdef __STDC__
  70. #include <stdio.h>
  71. #include <stdlib.h>
  72. #include <unistd.h>
  73. #include <stdarg.h>
  74. #include <time.h>
  75. #include <wait.h>
  76. #endif
  77.  
  78. #ifndef atarist
  79. #define    MAXLINE    1024            /* max message size */
  80. #else
  81. #define    MAXLINE    960            /* max message size */
  82. #endif
  83. #ifndef NULL
  84. #define NULL    0            /* manifest */
  85. #endif
  86.  
  87. #define IMPORTANT     LOG_ERR
  88.  
  89. #ifndef atarist
  90. static char    logname[] = "/dev/log";
  91. #else
  92. static char    logname[] = "/pipe/log";
  93. #endif
  94. static char    ctty[] = "/dev/console";
  95.  
  96. static int    LogFile = -1;        /* fd for log */
  97. static int    connected;        /* have done connect */
  98. static int    LogStat    = 0;        /* status bits, set by openlog() */
  99. static char    *LogTag = "syslog";    /* string to tag the entry with */
  100. static int    LogMask = 0xff;        /* mask of priorities to be logged */
  101. static int    LogFacility = LOG_USER;    /* default facility code */
  102.  
  103. #ifdef SYSLOG_INET
  104. static struct sockaddr_in SyslogAddr;    /*  address of loghost */
  105. #else
  106. #  ifdef SYSLOG_UNIXAF
  107. static struct sockaddr_un SyslogAddr;    /* AF_UNIX address of local logger */
  108. #  endif
  109. #endif
  110.  
  111. extern    int errno, sys_nerr;
  112. extern    char *sys_errlist[];
  113.  
  114. #ifdef __STDC__
  115. void syslog(int pri, const char *fmt, ...)
  116. #else
  117. syslog(pri, fmt, p0, p1, p2, p3, p4)
  118.     int pri;
  119.     char *fmt;
  120. #endif
  121. {
  122.     char buf[MAXLINE + 1], outline[MAXLINE + 1];
  123.     register char *b, *f, *o;
  124.     register int c;
  125.     long now;
  126.     int pid, olderrno = errno;
  127. #ifdef __STDC__
  128.     va_list argp;
  129.     va_start(argp, fmt);
  130. #endif
  131.     /* see if we should just throw out this message */
  132.     if ((unsigned) LOG_FAC(pri) >= LOG_NFACILITIES ||
  133.         LOG_MASK(LOG_PRI(pri)) == 0 ||
  134.         (pri &~ (LOG_PRIMASK|LOG_FACMASK)) != 0)
  135.         return;
  136.     if (LogFile < 0 || !connected)
  137.         openlog(LogTag, LogStat | LOG_NDELAY, 0);
  138.  
  139.     /* set default facility if none specified */
  140.     if ((pri & LOG_FACMASK) == 0)
  141.         pri |= LogFacility;
  142.  
  143.     /* build the message */
  144.     o = outline;
  145.     (void)sprintf(o, "<%d>", pri);
  146.     o += strlen(o);
  147.     time(&now);
  148.     (void)sprintf(o, "%.15s ", ctime(&now) + 4);
  149.     o += strlen(o);
  150.     if (LogTag) {
  151.         strcpy(o, LogTag);
  152.         o += strlen(o);
  153.     }
  154.     if (LogStat & LOG_PID) {
  155.         (void)sprintf(o, "[%d]", getpid());
  156.         o += strlen(o);
  157.     }
  158.     if (LogTag) {
  159.         strcpy(o, ": ");
  160.         o += 2;
  161.     }
  162.  
  163.     b = buf;
  164.     f = fmt;
  165.     while ((c = *f++) != '\0' && c != '\n' && b < &buf[MAXLINE]) {
  166.         if (c != '%') {
  167.             *b++ = c;
  168.             continue;
  169.         }
  170.         if ((c = *f++) != 'm') {
  171.             *b++ = '%';
  172.             *b++ = c;
  173.             continue;
  174.         }
  175.         if ((unsigned)olderrno > sys_nerr)
  176.             (void)sprintf(b, "error %d", olderrno);
  177.         else
  178.             strcpy(b, sys_errlist[olderrno]);
  179.         b += strlen(b);
  180.     }
  181.     *b++ = '\n';
  182.     *b = '\0';
  183. #ifdef __STDC__
  184.     (void)vsprintf(o, buf, argp);
  185. #else
  186.     (void)sprintf(o, buf, p0, p1, p2, p3, p4);
  187. #endif
  188.     c = strlen(outline);
  189.     if (c > MAXLINE)
  190.         c = MAXLINE;
  191.  
  192.     /* output the message to the local logger */
  193. #ifndef atarist
  194.     if (send(LogFile, outline, c, 0) >= 0)
  195. #else
  196.     /* force a context switch after writing (++tb) */
  197.     if (write(LogFile, outline, c) >= 0 && !Syield())
  198. #endif
  199.         return;
  200.     if (!(LogStat & LOG_CONS))
  201.         return;
  202.  
  203.     /* output the message to the console */
  204. #ifdef SYSV
  205.     pid = fork();
  206. #else
  207.     pid = vfork();
  208. #endif
  209.     if (pid == -1)
  210.         return;
  211.     if (pid == 0) {
  212.         int fd;
  213.  
  214. #ifdef SYSV
  215.         signal(SIGALRM, SIG_DFL);
  216. #else
  217.         sigsetmask(sigblock(0L) & ~sigmask(SIGALRM));
  218. #endif
  219.         alarm(5);
  220.         fd = open(ctty, O_WRONLY);
  221.         alarm(0);
  222.         strcat(o, "\r");
  223.         o = index(outline, '>') + 1;
  224.         write(fd, o, c + 1 - (o - outline));
  225.         close(fd);
  226.         _exit(0);
  227.     }
  228.     if (!(LogStat & LOG_NOWAIT))
  229.         while ((c = wait((int *)0)) > 0 && c != pid)
  230.             ;
  231. }
  232.  
  233. /*
  234.  * OPENLOG -- open system log
  235.  */
  236.  
  237. #ifdef __STDC__
  238. int openlog(const char *ident, int logstat, int logfac)
  239. #else
  240. openlog(ident, logstat, logfac)
  241.     char *ident;
  242.     int logstat, logfac;
  243. #endif
  244. {
  245. #ifdef SYSLOG_INET
  246.     struct servent *sp;
  247.     struct hostent *hp;
  248. #endif
  249.  
  250.     if (ident != NULL)
  251.         LogTag = ident;
  252.     LogStat = logstat;
  253.     if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
  254.         LogFacility = logfac;
  255. #ifdef SYSLOG_INET
  256.     if (LogFile >= 0)
  257.         return(0);
  258.     sp = getservbyname("syslog", "udp");
  259.     hp = gethostbyname(LOG_HOST);
  260.     if (sp != NULL && hp != NULL)
  261.     {
  262.         bzero(&SyslogAddr, sizeof(SyslogAddr));
  263.         SyslogAddr.sin_family = AF_INET;
  264.         bcopy(hp->h_addr, (char *)&SyslogAddr.sin_addr, hp->h_length);
  265.         SyslogAddr.sin_port = sp->s_port;
  266.         if (LogStat & LOG_NDELAY)
  267.             LogFile = socket(AF_INET, SOCK_DGRAM, 0);
  268.     }
  269.     if (LogFile != -1 && !connected &&
  270.         connect(LogFile, (struct sockaddr *) &SyslogAddr,
  271.            sizeof(SyslogAddr)) != -1)
  272.     {
  273.         connected = 1;
  274.         return(0);
  275.     }            
  276.     if (LogStat & LOG_NDELAY)
  277.         return(-1);
  278.     else            
  279.         return(0);
  280. #else
  281. #ifdef SYSLOG_UNIXAF
  282.     if (LogFile == -1) {
  283.         SyslogAddr.sun_family = AF_UNIX;
  284.         strncpy(SyslogAddr.sun_path, logname, sizeof SyslogAddr.sun_path);
  285.         if (LogStat & LOG_NDELAY) {
  286.             LogFile = socket(AF_UNIX, SOCK_DGRAM, 0);
  287.             fcntl(LogFile, F_SETFD, 1);
  288.         }
  289.     }
  290.     if (LogFile != -1 && !connected &&
  291.         connect(LogFile, (struct sockaddr *) &SyslogAddr,
  292.             sizeof(SyslogAddr.sun_family) +
  293.             strlen(SyslogAddr.sun_path)) >= 0)
  294.         connected = 1;
  295. #endif
  296. #ifdef atarist
  297.     if (LogFile == -1) {
  298.         LogFile = open(logname, O_RDWR);
  299.     }
  300.     if (LogFile >= 0  && !connected)
  301.     {
  302.         connected = 1;
  303.         return 1;
  304.     } else {
  305.         perror(logname);
  306.         connected = 0;
  307.         return -1;
  308.     }
  309. #endif
  310. #endif
  311. }
  312.  
  313.  
  314. /*
  315.  * CLOSELOG -- close the system log
  316.  */
  317.  
  318. #ifdef __STDC__
  319. void closelog(void)
  320. #else
  321. closelog()
  322. #endif
  323. {
  324.     (void) close(LogFile);
  325.     LogFile = -1;
  326.     connected = 0;
  327. }
  328.  
  329. /*
  330.  * SETLOGMASK -- set the log mask level
  331.  */
  332. #ifdef __STDC__
  333. int setlogmask(int pmask)
  334. #else
  335. setlogmask(pmask)
  336.     int pmask;
  337. #endif
  338. {
  339.     int omask;
  340.  
  341.     omask = LogMask;
  342.     if (pmask != 0)
  343.         LogMask = pmask;
  344.     return (omask);
  345. }
  346.